
import { getZxListApi } from "../../api/shesu"
import { formatDate } from "../../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    perPage: 10,
    debtorId: "",
    dateStart: "",
    dateEnd: "",
    total: 0,
    loading: false,
    query: "",
    searchInputClass: "input-is-focus",
    placeholderText: "请输入案号、企业名称、执行法院",
    contentList: [],
    dateVisible: false,
    defaultDate: Date.now(),
    dateValue: "",
    popupProps: {
      usingCustomNavbar: true,
    },
    start: "",
    end: "",
    current: 1, // 当前选择的时间代表开始时间1 结束时间 2
    dateStatus: 0
  },
  // 展示时间选择器
  showPicker() {
    let start = this.data.dateStart;
    let end = this.data.dateEnd;
    this.setData({
      dateVisible: true,
      current: 1,
      start,
      end,
      dateValue: start
    })
  },
  // 开始 结束时间选择切换
  currentSelect(e: any) {
    let current = e.currentTarget.dataset.current;
    if (current === 1 && this.data.start) {
      this.setData({
        dateValue: this.data.start
      })
    }
    if (current === 2 && this.data.end) {
      this.setData({
        dateValue: this.data.end
      })
    }
    this.setData({
      current
    })
  },
  // 日期选中值发生变化时触发
  onColumnChange(e: any) {
    if (this.data.current === 1) {
      this.setData({
        start: e.detail.value
      })
    } else {
      this.setData({
        end: e.detail.value
      })
    }

  },
  // 取消
  cancelbtn() {
    this.setData({
      start: "",
      dateStart: "",
      end: "",
      dateEnd: "",
      dateVisible: false
    })
    this.searchHandle();
  },
  // 确认
  ensurebtn() {
    let start = this.data.start;
    let end = this.data.end;
    if (this.data.current === 1) {
      start = this.data.start || formatDate(this.data.defaultDate);
    }
    if (this.data.current === 2) {
      end = this.data.end || formatDate(this.data.defaultDate);
    }

    this.setData({
      dateStart: start,
      dateEnd: end,
      start,
      end,
    }, () => {
      if (this.data.start && this.data.end) {
        if (new Date(this.data.start) <= new Date(this.data.end)) {
          this.setData({
            dateVisible: false
          })
          this.searchHandle()
        } else {
          wx.showToast({
            icon: "none",
            title: '开始时间不能大于结束时间',
          });
        }
      } else {
        this.setData({
          dateVisible: false
        })
        this.searchHandle()
      }
    })
  },
  // search-bar 搜索栏
  searchHandle() {
    this.data.page = 1;
    this.setData({
      contentList: []
    })
    this.getList();
  },
  // 确认搜索
  searchBlur() {
    this.searchHandle();
  },
  // 上拉到底
  scrolltolower() {
    if (this.data.total > this.data.contentList.length) {
      this.data.page++;
      this.getList();
    }
  },
  // 下拉刷新
  refresherrefresh() {
    this.searchHandle();
  },
  // 查看详情
  viewDetail(e: any) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/subPackages/packageA/shesu/executorDetail/executorDetail?id=" + id
    })
  },
  // 获取列表
  getList() {
    this.setData({
      loading: true
    })
    let para = {
      page: this.data.page,
      perPage: this.data.perPage,
      id: this.data.debtorId,
      query: this.data.query,
      dateStart: this.data.dateStart && this.data.dateStart + " 00:00:00",
      dateEnd: this.data.dateEnd && this.data.dateEnd + " 23:59:59"
    }

    getZxListApi(para).then((res: any) => {
      let contentList = res.content.list;
      this.data.total = res.content.total;
      this.setData({
        loading: false,
        contentList: this.data.contentList.concat(contentList),
        total: res.content.total
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    this.data.debtorId = option.debtorId;
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },


})